package com.hq.service;

import com.hq.bean.Article;
import com.hq.dao.ArticleDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ArticleServiceImpl implements ArticleService{

    @Resource
    private ArticleDao articleDao;

    @Override
    public List list(Map map) {
        return articleDao.list(map);
    }

    @Override
    public int add(Article article, Integer[] tids) {
        //添加主表
        int i = articleDao.add(article);
        //判断添加中间表
        if (tids!=null && tids.length>0){
            HashMap map = new HashMap();
            map.put("aid",article.getAid());
            map.put("tids",tids);
            articleDao.addArticletag(map);
        }
        return i;
    }

    @Override
    public int del(Integer aid) {
        //删除中间表
        articleDao.delArticletag(aid);
        //删除主表
        int i = articleDao.del(aid);

        return i;
    }

    @Override
    public int upd(Article article, Integer[] tids) {
        //修改主表
        int i = articleDao.upd(article);
        //删除中间
        articleDao.delArticletag(article.getAid());
        //判断添加中间
        if (tids!=null && tids.length>0){
            HashMap map = new HashMap();
            map.put("aid",article.getAid());
            map.put("tids",tids);
            articleDao.addArticletag(map);
        }
        return i;
    }

    @Override
    public Map getById(Integer aid) {
        return articleDao.getById(aid);
    }

}
